<template>
  <el-dialog v-model="visible" title="新建项目" width="500px" center append-to-body="true" @close="resetForm">
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      status-icon
      :rules="rules"
      label-width="90px"
    >
      <el-form-item label="项目名称" prop="name">
        <el-input v-model="ruleForm.name" />
      </el-form-item>
      <el-form-item label="项目标题" prop="desc">
        <el-input v-model="ruleForm.desc" />
      </el-form-item>
      <el-form-item label="项目框架" prop="basis">
        <el-select v-model="ruleForm.basis" style="width: 100%" placeholder="请选择">
          <el-option
            v-for="item in basisList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
            :disabled="item.disabled"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="项目平台" prop="platform">
        <el-select v-model="ruleForm.platform" style="width: 100%" placeholder="请选择">
          <el-option
            v-for="item in platformList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="resetForm">取消</el-button>
        <el-button type="primary" @click="submitForm">确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref, withDefaults, defineProps, computed, defineEmits } from 'vue'
import { FormInstance, ElMessage } from 'element-plus';
import request from "@/request";

const props = withDefaults(
	defineProps<{
		visible: boolean;
	}>(),
	{
		visible: false,
	}
);

const ruleFormRef = ref<FormInstance>();

const visible = computed(() => {
  return props.visible;
});

const emit = defineEmits(["close"]);

const ruleForm = reactive({
  name: '',
  desc: '',
  basis: '',
  platform: '',
});

const rules = reactive({
  name: [{ required: true, message: "请输入项目名称", trigger: 'blur' }],
  desc: [{ required: true, message: "请输入项目标题", trigger: 'blur' }],
  basis: [{ required: true, message: "请选择项目框架", trigger: 'blur' }],
  platform: [{ required: true, message: "请选择项目平台", trigger: 'blur' }],
});

const basisList = ref([
  { value: "vue3", label: "Vue3" },
  { value: "react", label: "React", disabled: true },
]);

const platformList = ref([
  { value: "web", label: "Web" },
  { value: "h5", label: "H5" }
]);

const submitForm = () => {
  if (!ruleFormRef) return
  ruleFormRef.value.validate(async (valid) => {
    if (valid) {
      console.log('submit!')
      const res = await request.request({
        url: "/api/project/create",
        method: "post",
        data: ruleForm
      });
      console.log("res", res);
      if (res && res.id) {
        ElMessage.success("创建成功");
        emit("close");
        window.location.reload();
      }
    } else {
      console.log('error submit!')
      return false
    }
  })
}

const resetForm = () => {
  if (!ruleFormRef) return
  ruleFormRef.value.resetFields()
  emit("close");
}
</script>

<style>

</style>